package com.mystudy.train.member.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.mystudy.train.common.exception.BusinessException;
import com.mystudy.train.common.exception.BusinessExceptionEnum;
import com.mystudy.train.common.util.JwtUtil;
import com.mystudy.train.common.util.SnowUtil;
import com.mystudy.train.member.domain.Member;
import com.mystudy.train.member.domain.MemberExample;
import com.mystudy.train.member.mapper.MemberMapper;
import com.mystudy.train.member.req.MemberLoginReq;
import com.mystudy.train.member.req.MemberRegisterReq;
import com.mystudy.train.member.req.MemberSendCodeReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mystudy.train.member.resp.MemberLoginResp;

import java.util.List;

/**
 * @author SaoE
 * @date 2025/1/7 22:01
 */
@Service
public class MemberService {

    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberMapper memberMapper;

    public int count() {
        return (int) memberMapper.countByExample(null);
    }

    public long register(MemberRegisterReq memberRegisterReq){
        String mobile = memberRegisterReq.getMobile();
        Member memberDB = selectByMobile(mobile);

        if (ObjectUtil.isNotEmpty(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();

    }

    public void MemberSendCode(MemberSendCodeReq memberSendCodeReq){
        LOG.info("查询数据库");
        String mobile = memberSendCodeReq.getMobile();
        Member memberDB = selectByMobile(mobile);

        if (ObjectUtil.isEmpty(memberDB)){
            LOG.info("未查到数据，用户信息插入数据库");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else{
            LOG.info("手机号存在，已存在用户数据");
        }

//        String code = RandomUtil.randomString(4);
        String code = "8888";
        LOG.info("生成验证码code：{}", code);

        LOG.info("保存短信记录表");

        LOG.info("对接短信通道");


    }

    public MemberLoginResp login(MemberLoginReq memberLoginReq){
        String mobile = memberLoginReq.getMobile();
        String code = memberLoginReq.getCode();
        Member memberDB = selectByMobile(mobile);

        if (ObjectUtil.isEmpty(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        if(!code.equals("8888")){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp memberLoginResp = new MemberLoginResp();
        memberLoginResp.setId(memberDB.getId());
        memberLoginResp.setMobile(mobile);

        String token = JwtUtil.createToken(memberDB.getId(), memberDB.getMobile());
        memberLoginResp.setToken(token);

        return memberLoginResp;
    }

    private Member selectByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);

        if (CollUtil.isEmpty(members)){
            return null;
        }else{
            return members.get(0);
        }


    }

}
